//
//  TTLiveService.swift
//  TTLive
//
//  Created by Toj on 2/2/21.
//

import Foundation

open class TTRTCService: NSObject {
    
    // 服务私有参数
    internal var appID: String = ""
    internal var token: String = ""
    
    // 如果是0: 声网是个很大的随机数
    internal var userID: TTUInt = 0
    internal var roomID: String = ""
    // 本地view
    internal weak var localView: TTView?
    
    public var logFilePath: String = ""
   
    // 动态创建远端View回调
    public var runTimeRemoteViewBlock: TTRTCRunTimeRemoteView?
    
    // 本地即将进入回调
    public var localWillJoinBlock: TTRTCLocalWillJoin?
    // 本地已经进入回调
    public var localDidJoinedBlock: TTRTCLocalDidJoined?
    // 本地重新进入回调
    public var localReJoinedBlock: TTRTCLocalDidReJoined?
    // 本地第一帧回调
    public var localFirstVideoDecodedBlock: TTRTCFirstVideoDecoded?
    
    // 远端进入回调
    public var remoteDidJoinedBlock: TTRTCRemoteDidJoined?
    // 远端第一帧回调
    public var remoteFirstVideoDecodedBlock: TTRTCFirstVideoDecoded?
    
    // 视频(关闭/打开)回调
    public var didVideoMutedBlock: TTRTCDidStreamMuted?
    // 音频(关闭/打开)回调
    public var didAudioMutedBlock: TTRTCDidStreamMuted?
    // 视频质量回调
    public var videoNetworkQualityBlock: TTRTCVideoNetworkQuality?
    // 音频质量回调
    public var audioVolumeIndicationBlock: TTRTCAudioVolumeIndication?
    
    // 有用户离开
    public var remoteDidOfflineBlock: TTRTCDidOffline?
    
    // Token已经失效
    public var tokenDidTimeoutBlock: TTRTCDidTimeout?
    // 发生错误回调
    public var didOccurErrorBlock: TTRTCDidOccurError?
    // 接收对方数据流消息错误的回调
    public var didOccurStreamMessageErrorBlock: TTRTCDidOccurStreamMessageError?
}

extension TTRTCService {
    
    internal func remoteDidJoined(uid: TTUInt, completion: TTRTCRunTimeHandler) {
        guard let view = runTimeRemoteViewBlock?(uid) else { return }
        
        completion(view)
    }
    
    internal func destroyServer() {
        runTimeRemoteViewBlock = nil
        localWillJoinBlock = nil
        localDidJoinedBlock = nil
        localReJoinedBlock = nil
        localFirstVideoDecodedBlock = nil
        remoteDidJoinedBlock = nil
        remoteFirstVideoDecodedBlock = nil
        didVideoMutedBlock = nil
        didAudioMutedBlock = nil
        videoNetworkQualityBlock = nil
        audioVolumeIndicationBlock = nil
        remoteDidOfflineBlock = nil
        tokenDidTimeoutBlock = nil
        didOccurErrorBlock = nil
        didOccurStreamMessageErrorBlock = nil
    }
}
